package wiki.mdzz.learn_android.controllers

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.core.content.edit
import wiki.mdzz.learn_android.databinding.ActivityFourthBinding
import wiki.mdzz.learn_android.utils.BaseActivity
import java.io.*

/*
private fun SharedPreferences.open(block: SharedPreferences.Editor.() -> Unit) {
    val editor = edit()
    editor.block()
    editor.apply()
}
 */

class FourthActivity : BaseActivity() {
    private lateinit var binding: ActivityFourthBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFourthBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val inputText = load()
        if (inputText.isNotEmpty()) {
            binding.editText.setText(inputText)
            binding.editText.setSelection(inputText.length)
            Toast.makeText(this, "Restoring succeed", Toast.LENGTH_SHORT).show()
        }

        binding.saveBtn.setOnClickListener {
            /* getSharedPreferences("data", Context.MODE_PRIVATE)
            .edit()
            .putString("name", "Tom")
            .putInt("age", 27)
            .putBoolean("married", true)
            .apply() */

            // custom higher order function
            /* getSharedPreferences("data", Context.MODE_PRIVATE).open {
              putString("name", "Tom")
              putInt("age", 27)
              putBoolean("married", true)
            } */
            getSharedPreferences("data", Context.MODE_PRIVATE).edit {
                putString("name", "Tom")
                putInt("age", 27)
                putBoolean("married", true)
            }
        }

        binding.loadBtn.setOnClickListener {
            val file = getSharedPreferences("data", Context.MODE_PRIVATE)
            val name = file.getString("name", "")
            val age = file.getInt("age", 0)
            val married = file.getBoolean("married", false)

            Log.d("FourthActivity", "name is $name")
            Log.d("FourthActivity", "name is $age")
            Log.d("FourthActivity", "name is $married")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        val inputText = binding.editText.text.toString()
        save(inputText)
    }

    private fun load(): String {
        val content = StringBuilder()
        try {
            val input = openFileInput("data")
            val reader = BufferedReader(InputStreamReader(input))
            reader.use { reader.forEachLine { content.append(it) } }
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return content.toString()
    }

    private fun save(inputText: String) {
        try {
            val output = openFileOutput("data", Context.MODE_PRIVATE)
            val writer = BufferedWriter(OutputStreamWriter(output))
            writer.use { it.write(inputText) }
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

